<?php 
/*
 * Server3rdModel
 * 
 */
namespace Home\Model;
header("Content-Type:text/html;charset=utf-8");

use Common\Library\BusinessException;
use Common\Library\Constant;

use Think\Cache\Driver\Redis;
use Think\Log;
use Think\Model;

class Server3rdModel{
    //查看是否授权
    public function server3rd_post($url,$xml_data){
        $sign = $this->genSign($xml_data);
        $xml_data['sign'] = $sign;
        // var_dump($url);die;

        $header[] = "content-type:application/json;charset=utf8";
        // $header[] = 'josn: {"uid":"1","sid":"0123456789ABCDEF0123456789ABCDEF","request":{},"ver":"1"}';
            $ch = curl_init($url);
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_TIMEOUT, 30);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($xml_data));
            $response = curl_exec($ch);
        if(curl_errno($ch))
        {
            Log::write("server3rd_post**************(curl_errno(ch):" . curl_errno($ch) ."**************curl_error(ch):" . curl_error($ch));
            print curl_error($ch);
        }
        curl_close($ch);
        Log::write("server3rd发送：".json_encode($response, JSON_UNESCAPED_UNICODE), Log::INFO);
        return $response;
    }

    function genSign(array $param) {
        //0. 删除原数据中自带的sign值,防止干扰计算结果
        unset($param['sign']);
        //1. 按key由a到z排序
        ksort($param);
        foreach ($param as $key => $value) {
            if (is_array($value)) {
                $param[$key] = $this->genSign($value);
            }
        }
        //2. 生成以&符链接的key=value形式的字符串
        $paramString = urldecode(http_build_query($param));
        //3. 拼接我们的服务秘钥，并md5加密
        $sign = md5($paramString . 'secret@huoyan.com');
        return $sign;
    }
    
}
?>